package com.app.notificationmodule

import android.app.*
import android.app.Notification.EXTRA_NOTIFICATION_ID
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.BitmapFactory
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.RemoteInput
import com.alibaba.android.arouter.facade.annotation.Route
import com.app.common.activity.BaseActivity
import com.app.common.context
import com.app.notificationmodule.activity.BubbleActivity
import com.app.notificationmodule.receiver.NotificationReceiver
import com.app.notificationmodule.service.NotificationService
import kotlinx.android.synthetic.main.activity_notification.*

@Route(path = "/notification_module/notification_activity")
class NotificationActivity : BaseActivity() {

    private val CHANNEL_ID = "11111"

    private val ACTION_SNOOZE = "com.app.notification"

    private lateinit var receiver: NotificationReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_notification)

        receiver = NotificationReceiver()
        val intentFilter = IntentFilter()
        intentFilter.addAction(ACTION_SNOOZE)

        registerReceiver(receiver, intentFilter)

        val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        activity_notification_ordinary.setOnClickListener {

            // Create an explicit intent for an Activity in your app
            val intent = Intent(this, NotificationActivity::class.java).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            }
            val pendingIntent: PendingIntent = PendingIntent.getActivity(this@NotificationActivity, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT)

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val name = "notification"
                val descriptionText = "description"
                val importance = NotificationManager.IMPORTANCE_HIGH
                val channel = NotificationChannel(CHANNEL_ID, name, importance)
                notificationManager.createNotificationChannel(channel)
            }

            val snoozeIntent = Intent(this, NotificationService::class.java).apply {
                action = ACTION_SNOOZE
                putExtra(EXTRA_NOTIFICATION_ID, 0)
            }
            val snoozePendingIntent: PendingIntent =
                    PendingIntent.getService(this, 0, snoozeIntent, 0)
            var remoteInput: RemoteInput = RemoteInput.Builder("key_text_reply").run {
                setLabel("回复一")
                build()
            }

            var action: NotificationCompat.Action =
                    NotificationCompat.Action.Builder(R.mipmap.notification_icon,
                            "回复", snoozePendingIntent)
                            .addRemoteInput(remoteInput)
                            .build()

            val remoteView = RemoteViews(packageName, R.layout.activity_bubble)

            var builder = NotificationCompat.Builder(this@NotificationActivity, CHANNEL_ID)
                    .setSmallIcon(R.drawable.main_bottom_personal_unselected)
                    .setContentTitle("Notification")
                    .setContentInfo("补充内容")
                    .setTicker("新消息")
                    .setWhen(System.currentTimeMillis())
                    .setContentText("send one Message!!!!!")
                    //.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.main_bottom_personal_unselected))
                    .setAutoCancel(true)
                    //.setFullScreenIntent(pendingIntent,true)
                    //.setCustomHeadsUpContentView(remoteView)
                    //.addAction(action)
                    //.setPriority(NotificationManager.IMPORTANCE_DEFAULT)


            notificationManager.notify(1, builder.build())
        }

        activity_notification_bubbles.setOnClickListener {

            // Create bubble intent
            val target = Intent(this@NotificationActivity, BubbleActivity::class.java)
            val bubbleIntent = PendingIntent.getActivity(this@NotificationActivity, 0, target, 0 /* flags */)

            val intent = Intent(this, NotificationActivity::class.java).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            }
            val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)



            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

                val name = "notification"
                val descriptionText = "description"
                val importance = NotificationManager.IMPORTANCE_DEFAULT
                val channel = NotificationChannel("22222", name, importance).apply {
                    description = descriptionText
                }
                notificationManager.createNotificationChannel(channel)

                // Create bubble metadata
                val bubbleData = Notification.BubbleMetadata.Builder()
                        .setDesiredHeight(600)
                        .setIcon(Icon.createWithResource(this@NotificationActivity, R.mipmap.notification_icon))
                        .setIntent(bubbleIntent)
                        .build()

                // Create notification
                val chatBot = Person.Builder()
                        .setBot(true)
                        .setName("BubbleBot")
                        .setImportant(true)
                        .build()

                val builder = Notification.Builder(this@NotificationActivity, "22222")
                        .setContentIntent(pendingIntent)
                        .setSmallIcon(R.mipmap.notification_icon)
                        .setBubbleMetadata(bubbleData)
                        .addPerson(chatBot)

                notificationManager.notify(2, builder.build())
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(receiver)
    }

}